package com.qzsoft.zhjd.server.modules.cement.handler;

import java.math.BigDecimal;

import com.qzsoft.zhjd.server.common.utils.NumberUtils;
import com.qzsoft.zhjd.server.modules.cement.entity.CollectCementDataRatioEntity;
import com.qzsoft.zhjd.server.modules.cement.entity.CollectCementMatchEntity;
import com.qzsoft.zhjd.server.modules.cement.model.CementCollectDataModel;

/**
 * @ClassName CementDataRatioHandler
 * @Description 生产占比处理
 * @author linhuawei
 * @since 2022-02-28
 */
public class CementDataRatioHandler {

    /**
     * @Title coverTo
     * @Description 生产占比处理
     * @param originalData
     * @param batchWeight
     * @param match
     * @return
     */
    public static CollectCementDataRatioEntity coverTo(CementCollectDataModel originalData, BigDecimal batchWeight,
                                                       CollectCementMatchEntity match) {
        // 处理占比数据
        CollectCementDataRatioEntity dataRatio = new CollectCementDataRatioEntity();

        // 计算实际占比
        dataRatio.setGravel1RatioAct(NumberUtils.countRatio(batchWeight, originalData.getGravel1Act(), 1));
        dataRatio.setGravel2RatioAct(NumberUtils.countRatio(batchWeight, originalData.getGravel2Act(), 1));
        dataRatio.setGravel3RatioAct(NumberUtils.countRatio(batchWeight, originalData.getGravel3Act(), 1));
        dataRatio.setGravel4RatioAct(NumberUtils.countRatio(batchWeight, originalData.getGravel4Act(), 1));
        dataRatio.setGravel5RatioAct(NumberUtils.countRatio(batchWeight, originalData.getGravel5Act(), 1));
        dataRatio.setGravel6RatioAct(NumberUtils.countRatio(batchWeight, originalData.getGravel6Act(), 1));
        dataRatio.setPowder1RatioAct(NumberUtils.countRatio(batchWeight, originalData.getPowder1Act(), 1));
        dataRatio.setPowder2RatioAct(NumberUtils.countRatio(batchWeight, originalData.getPowder1Act(), 1));
        dataRatio.setWaterRatioAct(NumberUtils.countRatio(batchWeight, originalData.getGravel1Act(), 1));

        // 根据是否有配方信息，判读如何处理理论占比
        if (null != match) {
            // 理论占比 = 配方占比
            dataRatio.setGravel1RatioTheo(match.getGravel1());
            dataRatio.setGravel2RatioTheo(match.getGravel2());
            dataRatio.setGravel3RatioTheo(match.getGravel3());
            dataRatio.setGravel4RatioTheo(match.getGravel4());
            dataRatio.setGravel5RatioTheo(match.getGravel5());
            dataRatio.setGravel6RatioTheo(match.getGravel6());
            dataRatio.setPowder1RatioTheo(match.getPowder1());
            dataRatio.setPowder2RatioTheo(match.getPowder2());
            dataRatio.setWaterRatioTheo(match.getWater());
        } else {
            // 理论占比 = 采集到的理论占比
            dataRatio.setGravel1RatioTheo(originalData.getGravel1RatioTheo());
            dataRatio.setGravel2RatioTheo(originalData.getGravel2RatioTheo());
            dataRatio.setGravel3RatioTheo(originalData.getGravel3RatioTheo());
            dataRatio.setGravel4RatioTheo(originalData.getGravel4RatioTheo());
            dataRatio.setGravel5RatioTheo(originalData.getGravel5RatioTheo());
            dataRatio.setGravel6RatioTheo(originalData.getGravel6RatioTheo());
            dataRatio.setPowder1RatioTheo(originalData.getPowder1RatioTheo());
            dataRatio.setPowder2RatioTheo(originalData.getPowder1RatioTheo());
            dataRatio.setWaterRatioTheo(originalData.getWaterRatioTheo());
        }

        // 占比误差
        dataRatio.setGravel1RatioError(countError(dataRatio.getGravel1RatioAct(), dataRatio.getGravel1RatioTheo()));
        dataRatio.setGravel2RatioError(countError(dataRatio.getGravel2RatioAct(), dataRatio.getGravel2RatioTheo()));
        dataRatio.setGravel3RatioError(countError(dataRatio.getGravel3RatioAct(), dataRatio.getGravel3RatioTheo()));
        dataRatio.setGravel4RatioError(countError(dataRatio.getGravel4RatioAct(), dataRatio.getGravel4RatioTheo()));
        dataRatio.setGravel5RatioError(countError(dataRatio.getGravel5RatioAct(), dataRatio.getGravel5RatioTheo()));
        dataRatio.setGravel6RatioError(countError(dataRatio.getGravel6RatioAct(), dataRatio.getGravel6RatioTheo()));
        dataRatio.setPowder1RatioError(countError(dataRatio.getPowder1RatioAct(), dataRatio.getPowder1RatioTheo()));
        dataRatio.setPowder2RatioError(countError(dataRatio.getPowder2RatioAct(), dataRatio.getPowder2RatioTheo()));
        dataRatio.setWaterRatioError(countError(dataRatio.getWaterRatioAct(), dataRatio.getWaterRatioTheo()));

        // 返回数据
        return dataRatio;
    }

    /**
     * @Title countError
     * @Description 占比误差
     * @param act
     * @param theo
     * @return
     */
    private static BigDecimal countError(BigDecimal act, BigDecimal theo) {
        if (null == act) {
            act = BigDecimal.ZERO;
        }
        if (null == theo) {
            theo = BigDecimal.ZERO;
        }
        return act.subtract(theo);
    }

}
